//
//  AttentionView.m
//  MiBaoTu
//
//  Created by admin on 16/9/19.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "AttentionView.h"

@interface AttentionView () {
    UIWindow            *_selectWindow;
    UILabel             *_contentLb;
    UIButton            *_noBt;
    UIButton            *_cancelBt;
}

@property (nonatomic,strong)NSString    *titleString;
@property (nonatomic,strong)NSString    *sureString;
@property (nonatomic,strong)NSString    *cancelString;

@end
@implementation AttentionView


+ (AttentionView *)defualtStander
{
    static AttentionView *atttentionView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        atttentionView = [AttentionView new];
    });
    return atttentionView;
}

- (id)initWithTitle:(NSString *)title withCancelBt:(NSString *)cancel withSureBt:(NSString *)sure
{
    self = [super init];
    if (self) {
        self.titleString = title;
        self.sureString = sure;
        self.cancelString = cancel;
        
        [self onCreateUI];
    }
    return self;
}


- (void)onCreateUI
{
    self.frame = CGRectMake((SCREEN_WIDTH-400*PERCEN)/2., 480*PERCEN, 400*PERCEN, 300*PERCEN);
    if (ISIPAD) {
        self.frame = CGRectMake((SCREEN_WIDTH-600*PERCEN)/2., (SCREEN_HEIGHT-450*PERCEN)/2., 600*PERCEN, 450*PERCEN);
    }
    self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 6.f;
    self.layer.masksToBounds = YES;
    self.userInteractionEnabled = YES;
    
    //内容
    _contentLb = [[UILabel alloc]initWithFrame:CGRectMake(44*PERCEN, 0, self.width-2*44*PERCEN, (ISIPAD?350*PERCEN:219*PERCEN))];
    _contentLb.backgroundColor = [UIColor clearColor];
    _contentLb.textAlignment = NSTextAlignmentCenter;
    _contentLb.textColor = UIColorFromHex(0x333333);
    _contentLb.font = [UIFont systemFontOfSize:13.f];
    _contentLb.numberOfLines = 0;
    _contentLb.text = (self.titleString.length != 0 ? self.titleString : @"你是否要取消关注该用户?");
    float contentHight = [_contentLb.text boundingRectWithSize:CGSizeMake(_contentLb.width, 1000) options:1|1 attributes:@{NSFontAttributeName:_contentLb.font} context:nil].size.height;
    if (contentHight <= 20) {
        //只有一行
        _contentLb.textAlignment = NSTextAlignmentCenter;
    }
    NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:_contentLb.text];
    NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle1 setLineSpacing:15*PERCEN];
    paragraphStyle1.alignment = _contentLb.textAlignment;
    [attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [_contentLb.text length])];
    [_contentLb setAttributedText:attributedString1];
    [self addSubview:_contentLb];
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, _contentLb.bottom, self.width, 1)];
    lineView.backgroundColor = UIColorFromHex(0xcccccc);
    [self addSubview:lineView];
    
    _noBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _noBt.frame = CGRectMake(0, lineView.bottom, self.width/2., self.height-_contentLb.height);
    [_noBt setTitle:self.sureString forState:UIControlStateNormal];
    [_noBt setTitle:self.sureString forState:UIControlStateHighlighted];
    [_noBt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_noBt setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
    _noBt.titleLabel.font = [UIFont systemFontOfSize:14.f];
    [_noBt addTarget:self action:@selector(onSelectItemForSure) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_noBt];
    
    UIView *linesView = [[UIView alloc]initWithFrame:CGRectMake(self.width/2., lineView.bottom, 1, _noBt.height)];
    linesView.backgroundColor = UIColorFromHex(0xcccccc);
    [self addSubview:linesView];
    
    _cancelBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _cancelBt.frame = CGRectMake(self.width/2., lineView.bottom, self.width/2., _noBt.height);
    [_cancelBt setTitle:self.cancelString forState:UIControlStateNormal];
    [_cancelBt setTitle:self.cancelString forState:UIControlStateHighlighted];
    [_cancelBt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_cancelBt setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
    _cancelBt.titleLabel.font = [UIFont systemFontOfSize:14.f];
    [_cancelBt addTarget:self action:@selector(onSelectItemForCancel) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_cancelBt];
}

//修改UI，定位删除
- (void)onChangeUIForDelete
{
//    _contentLb.text = @"";
////    _contentLb.text = @"是否确认删除?";
//    [_noBt setTitle:@"取消" forState:UIControlStateNormal];
//    [_noBt setTitle:@"取消" forState:UIControlStateHighlighted];
//    [_cancelBt setTitle:@"确认" forState:UIControlStateNormal];
//    [_cancelBt setTitle:@"确认" forState:UIControlStateHighlighted];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self onChangeUIForDelete];
}

- (void)showInWindow
{
    if (!_selectWindow) {
        _selectWindow = [[UIWindow alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.frame];
        _selectWindow.backgroundColor = [UIColorFromHex(0x000000) colorWithAlphaComponent:.2];
        [self onTurnToOriention:_orientation];
        [_selectWindow makeKeyAndVisible];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onTapForHideSelf)];
        [_selectWindow addGestureRecognizer:tap];
    }
    [_selectWindow addSubview:self];
}

- (void)orientChange:(NSNotification *)noti {
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
//    [self onTurnToOriention:orientation];
}

- (void)onTurnToOriention:(UIDeviceOrientation)orientation
{
    
    if (ISIPAD) {
        _selectWindow.transform = CGAffineTransformMakeRotation(M_PI*.5);
        _selectWindow.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH);
        if (ISIPAD) {
            self.frame = CGRectMake((SCREEN_WIDTH-600*PERCEN)/2., _selectWindow.centery, 600*PERCEN, 450*PERCEN);
        } else {
            self.frame = CGRectMake((SCREEN_WIDTH-400*PERCEN)/2., _selectWindow.centery+150*PERCEN, 400*PERCEN, 300*PERCEN);
        }
        
    } else {
        _selectWindow.transform = CGAffineTransformMakeRotation(0);
        _selectWindow.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        if (ISIPAD) {
            self.frame = CGRectMake((SCREEN_WIDTH-600*PERCEN)/2., (SCREEN_HEIGHT-450*PERCEN)/2., 600*PERCEN, 450*PERCEN);
        } else {
            self.frame = CGRectMake((SCREEN_WIDTH-400*PERCEN)/2., (SCREEN_HEIGHT-300*PERCEN)/2., 400*PERCEN, 300*PERCEN);
        }
    }
}


- (void)onSelectItemForSure
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectSureItem:)]) {
        [_delegate onSelectSureItem:self];
        [self removeFromWindow];
    }
}

- (void)onSelectItemForCancel
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectCancelItem:)]) {
        [_delegate onSelectCancelItem:self];
        [self removeFromWindow];
    }
}

- (void)removeFromWindow
{
    [self removeFromSuperview];
    NSMutableArray *windows = [[NSMutableArray alloc]initWithArray:[UIApplication sharedApplication].windows];
    if ([windows containsObject:_selectWindow]) {
        [windows removeObject:_selectWindow];
        _selectWindow = nil;
    }
    
    [windows enumerateObjectsUsingBlock:^(UIWindow  *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.windowLevel == UIWindowLevelNormal && [obj isKindOfClass:[UIWindow class]]) {
            [obj makeKeyAndVisible];
            *stop = YES;
        }
    }];
}

- (void)onTapForHideSelf
{
    [self removeFromWindow];
}

@end
